// var n;
// var $pPSDK_Config = require("./AL_Framework_AL_SDK_CONFIG");
// var $macros = require("./AL_Framework_Macros");
// var l = cc._decorator;
// var d = l.ccclass;
// var u = l.property;
// var h = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.panel = null;
//         t.bar = null;
//         t.label = null;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.onLoad = function () {
//         cc.tween(this.bar)
//             .by(1, {
//                 angle: -360
//             })
//             .repeatForever()
//             .start();
//     };
//     t.prototype.show = function () {
//         this.node.active = !0;
//     };
//     t.prototype.hide = function () {
//         this.node.active = !1;
//     };
//     t.prototype.gengxin_jindu = function (e) {
//         if (this.label) {
//             this.label.string = e + "%";
//         }
//     };
//     __decorate([u(cc.Node)], t.prototype, "panel", void 0);
//     __decorate([u(cc.Node)], t.prototype, "bar", void 0);
//     __decorate([u(cc.Node)], t.prototype, "label", void 0);
//     return __decorate([d], t);
// })(cc.Component);
// var p = (function () {
//     function e() {
//         this.logTag = "[AL_Framework_UI_Loading]";
//         this._timeout_callback = null;
//         this._timeout_id = -1;
//         this._ui_name = "";
//     }
//     e.Instance = function () {
//         return this._instance || (this._instance = new e());
//     };
//     e.prototype.jiazai_yuzhiti = function () {
//         var e = this;
//         return new Promise(function (t, o) {
//             var i = "HUA/framework/common/components/ui_Loading/ui_Loading";
//             Manager.cacheManager.yibuHuoquHuancun(i, cc.Prefab, $macros.Macro.BUNDLE_RESOURCES).then(function (n) {
//                 n
//                     ? (Manager.assetManager.addPersistAsset(i, n, $macros.Macro.BUNDLE_RESOURCES),
//                       e.shezhi_yuzhiti(n, h),
//                       t(n))
//                     : (Log.e(e.logTag, "AL_Framework_UI_Loading 预制体加载失败:" + i), o());
//             });
//         });
//     };
//     e.prototype.shezhi_yuzhiti = function (e, t) {
//         this._huoqu_yuzhiti_xinxi(e).getComponent(t)
//             ? ((this._prefab = e), (this._type = t))
//             : Log.e(this.logTag, "未挂载" + t.prototype.constructor.name + ",将无法使用，请排查");
//     };
//     e.prototype.init = function () {
//         return $pPSDK_Config.Config.USE_SYS_UILOADING ? this.jiazai_yuzhiti() : Promise.resolve();
//     };
//     e.prototype.show = function (e) {
//         var t = this;
//         if (void 0 === e) {
//             e = {};
//         }
//         if (this._prefab) {
//             this._option = e;
//             this._ui_name = e.uiName || "";
//             if (!this._loading) {
//                 var o = cc.instantiate(this._prefab);
//                 Manager.uiManager.tianjia_shitu(o, 50);
//                 this._loading = o.getComponent(this._type);
//             }
//             if ("number" != typeof e.timeout) {
//                 e.timeout = $pPSDK_Config.Config.LOAD_VIEW_TIME_OUT;
//             }
//             var i = this._loading.show();
//             i instanceof Promise
//                 ? i.then(function () {
//                       if (t._show_callback) {
//                           t._show_callback();
//                       }
//                       t._kaishiDaojishi(e.timeout);
//                   })
//                 : (this._show_callback && this._show_callback(), this._kaishiDaojishi(e.timeout));
//             if ("number" != typeof e.delay) {
//                 e.delay = $pPSDK_Config.Config.LOAD_VIEW_DELAY;
//             }
//             if (e.delay > 0) {
//                 this._loading.node.opacity = 0;
//                 this._loading.scheduleOnce(function () {
//                     t._loading.node.opacity = 255;
//                 }, e.delay);
//             }
//         } else {
//             Log.e(this.logTag, "未加载系统UILoading组件");
//         }
//     };
//     e.prototype.hide = function (e) {
//         var t = this;
//         this._prefab && this._loading
//             ? (e && (this._hide_callback = e),
//               this._hide().then(function () {
//                   if (t._hide_callback) {
//                       t._hide_callback();
//                   }
//               }))
//             : Log.e(this.logTag, "未加载系统Loading组件");
//     };
//     e.prototype.close = function () {
//         clearTimeout(this._timeout_id);
//         this._timeout_id = -1;
//         this._loading.node.active = !1;
//     };
//     e.prototype.gengxin_jindu = function (e) {
//         if (this._loading) {
//             this._loading.gengxin_jindu(e);
//         }
//     };
//     e.prototype._hide = function () {
//         clearTimeout(this._timeout_id);
//         this._timeout_id = -1;
//         var e = this._loading.hide();
//         return e instanceof Promise ? e : Promise.resolve();
//     };
//     e.prototype._kaishiDaojishi = function (e) {
//         var t = this;
//         if (e > 0) {
//             this._timeout_id = setTimeout(function () {
//                 t._timeout_callback
//                     ? t._timeout_callback()
//                     : Manager.alert.show({
//                           tag: 1001,
//                           title: "加载超时",
//                           content: "加载界面" + (t._ui_name ? t._ui_name : "") + "超时，请重试",
//                           confirmText: "重试",
//                           showCancel: !0,
//                           confirm: function () {
//                               t.close();
//                               t.show(t._option);
//                           },
//                           cancel: function () {
//                               t.close();
//                           }
//                       });
//             }, 1e3 * e);
//         }
//     };
//     e.prototype._huoqu_yuzhiti_xinxi = function (e) {
//         return e instanceof cc.Prefab ? e.data : e;
//     };
//     e._instance = null;
//     return e;
// })();
// exports.default = p;


// 引入所需模块和类
const AL_Framework_AL_SDK_CONFIG = require("./AL_Framework_AL_SDK_CONFIG");
const AL_Framework_Macros = require("./AL_Framework_Macros");
const { ccclass, property } = cc._decorator;

/**
 * UILoadingPanel 类
 * 负责管理加载界面的逻辑
 */
@ccclass
class UILoadingPanel extends cc.Component {
    @property(cc.Node) panel = null;
    @property(cc.Node) bar = null;
    @property(cc.Node) label = null;

    onLoad() {
        // 使用Cocos Creator的tween动画系统，使进度条旋转
        cc.tween(this.bar)
            .by(1, { angle: -360 })
            .repeatForever()
            .start();
    }

    /**
     * 显示加载界面
     */
    show() {
        this.node.active = true;
    }

    /**
     * 隐藏加载界面
     */
    hide() {
        this.node.active = false;
    }

    /**
     * 更新进度
     * @param {number} progress - 当前进度百分比
     */
    gengxin_jindu(progress) {
        if (this.label) {
            this.label.string = `${progress}%`;
        }
    }
}

/**
 * UILoadingManager 类
 * 单例模式管理 UILoadingPanel 的加载与显示
 */
class UILoadingManager {
    constructor() {
        this.logTag = "[AL_Framework_UI_Loading]";
        this._timeout_callback = null;
        this._timeout_id = -1;
        this._ui_name = "";
        this._option = {};
        this._instance = null;
        this._prefab = null;
        this._type = null;
        this._loading = null;
        this._show_callback = null;
        this._hide_callback = null;
    }

    /**
     * 获取单例实例
     * @returns {UILoadingManager} - UILoadingManager 实例
     */
    static Instance() {
        if (!this._instance) {
            this._instance = new UILoadingManager();
        }
        return this._instance;
    }

    /**
     * 异步加载 AL_Framework_UI_Loading 预制体
     * @returns {Promise<cc.Prefab>} - Promise 对象，返回加载的预制体
     */
    jiazai_yuzhiti() {
        const prefabPath = "HUA/framework/common/components/ui_Loading/ui_Loading";
        return new Promise((resolve, reject) => {
            Manager.cacheManager.yibuHuoquHuancun(prefabPath, cc.Prefab, AL_Framework_Macros.Macro.BUNDLE_RESOURCES)
                .then(prefab => {
                    if (prefab) {
                        Manager.assetManager.addPersistAsset(prefabPath, prefab, AL_Framework_Macros.Macro.BUNDLE_RESOURCES);
                        this.shezhi_yuzhiti(prefab, UILoadingPanel);
                        resolve(prefab);
                    } else {
                        Log.e(this.logTag, `AL_Framework_UI_Loading 预制体加载失败: ${prefabPath}`);
                        reject();
                    }
                });
        });
    }

    /**
     * 设置预制体
     * @param {cc.Prefab} prefab - 预制体
     * @param {function} type - 组件类类型
     */
    shezhi_yuzhiti(prefab, type) {
        if (this._huoqu_yuzhiti_xinxi(prefab).getComponent(type)) {
            this._prefab = prefab;
            this._type = type;
        } else {
            Log.e(this.logTag, `未挂载${type.name},将无法使用，请排查`);
        }
    }

    /**
     * 初始化 UILoadingManager
     * @returns {Promise} - 返回 Promise 对象
     */
    init() {
        return AL_Framework_AL_SDK_CONFIG.Config.USE_SYS_UILOADING ? this.jiazai_yuzhiti() : Promise.resolve();
    }

    /**
     * 显示加载界面
     * @param {object} [option={}] - 配置选项
     */
    show(option = {}) {
        if (this._prefab) {
            this._option = option;
            this._ui_name = option.uiName || "";

            if (!this._loading) {
                let instance = cc.instantiate(this._prefab);
                Manager.uiManager.tianjia_shitu(instance, 50);
                this._loading = instance.getComponent(this._type);
            }

            if (typeof option.timeout !== 'number') {
                option.timeout = AL_Framework_AL_SDK_CONFIG.Config.LOAD_VIEW_TIME_OUT;
            }

            let showResult = this._loading.show();
            if (showResult instanceof Promise) {
                showResult.then(() => {
                    if (this._show_callback) this._show_callback();
                    this._kaishiDaojishi(option.timeout);
                });
            } else {
                if (this._show_callback) this._show_callback();
                this._kaishiDaojishi(option.timeout);
            }

            if (typeof option.delay !== 'number') {
                option.delay = AL_Framework_AL_SDK_CONFIG.Config.LOAD_VIEW_DELAY;
            }

            if (option.delay > 0) {
                this._loading.node.opacity = 0;
                this._loading.scheduleOnce(() => {
                    this._loading.node.opacity = 255;
                }, option.delay);
            }
        } else {
            Log.e(this.logTag, "未加载系统UILoading组件");
        }
    }

    /**
     * 隐藏加载界面
     * @param {function} [callback] - 隐藏后的回调函数
     */
    hide(callback) {
        if (this._prefab && this._loading) {
            if (callback) this._hide_callback = callback;
            this._hide().then(() => {
                if (this._hide_callback) this._hide_callback();
            });
        } else {
            Log.e(this.logTag, "未加载系统Loading组件");
        }
    }

    /**
     * 关闭加载界面
     */
    close() {
        clearTimeout(this._timeout_id);
        this._timeout_id = -1;
        this._loading.node.active = false;
    }

    /**
     * 更新加载进度
     * @param {number} progress - 当前进度百分比
     */
    gengxin_jindu(progress) {
        if (this._loading) {
            this._loading.gengxin_jindu(progress);
        }
    }

    /**
     * 隐藏加载界面
     * @returns {Promise} - 返回 Promise 对象
     */
    _hide() {
        clearTimeout(this._timeout_id);
        this._timeout_id = -1;
        let hideResult = this._loading.hide();
        return hideResult instanceof Promise ? hideResult : Promise.resolve();
    }

    /**
     * 启动超时计时器
     * @param {number} timeout - 超时时间，单位为秒
     */
    _kaishiDaojishi(timeout) {
        if (timeout > 0) {
            this._timeout_id = setTimeout(() => {
                this._timeout_callback
                    ? this._timeout_callback()
                    : Manager.alert.show({
                          tag: 1001,
                          title: "加载超时",
                          content: `加载界面${this._ui_name ? this._ui_name : ""}超时，请重试`,
                          confirmText: "重试",
                          showCancel: true,
                          confirm: () => {
                              this.close();
                              this.show(this._option);
                          },
                          cancel: () => {
                              this.close();
                          }
                      });
            }, 1000 * timeout);
        }
    }

    /**
     * 获取预制体信息
     * @param {cc.Prefab} prefab - 预制体
     * @returns {cc.Prefab} - 预制体数据
     */
    _huoqu_yuzhiti_xinxi(prefab) {
        return prefab instanceof cc.Prefab ? prefab.data : prefab;
    }
}

// 导出默认类
exports.default = UILoadingManager;
